import moment from "moment";
import { REDIS_KEY } from "../declare/keys";
import { Gift, Danmaku } from "../declare/liver";
import { getRedisClient } from "../library/redis";
import { GetUserInfo } from "../service/getUserInfoByid";
import { saveDataUniq, saveData } from "../service/mongoUtil";
import RabbitMq from "../library/rabbitmq";
import { uniqueId } from "lodash";
const rabbitlib = new RabbitMq();
export async function updateLiverData(id: number) {
  const doc = await GetUserInfo(id);
  await saveDataUniq(doc, "t_liver_base_info");
}

export async function updataLiverGiftData(gift: Gift, id: number) {
  const redis = getRedisClient();
  const datePrefix = moment().format("YYYY-MM-DD");
  const gfitWater = {
    roomId: id,
    timestamp: gift.sendTimeMs.toNumber(),
    value: gift.value,
    count: gift.count,
    combo: gift.combo,
    giftName: gift.giftName,
    giftId: gift.giftId,
    user: {
      userId: gift.user.userId.toNumber(),
      nickname: gift.user.nickname,
      badge: gift.user.badge,
    },
  };
  await Promise.all([
    saveData(gfitWater, "t_liver_gift_water"),
    redis.zincrby(REDIS_KEY.REDIS_KEY_GIFT_RANK, gift.value, id.toString()),
    redis.zincrby(`${datePrefix}:${REDIS_KEY.REDIS_KEY_GIFT_RANK}`, gift.value, id.toString()),
    rabbitlib.sendGift({
      roomId: id,
      id: uniqueId(),
      userId: gfitWater.user.userId,
      value: gfitWater.value,
      timestamp: gfitWater.timestamp,
    }),
  ]);
}

export async function updateDanmakuData(danmaku: Danmaku, roomId: number) {
  await rabbitlib.sendDanmaku({
    id: uniqueId(),
    timestamp: danmaku.sendTimeMs.toNumber(),
    roomId,
    userId: danmaku.userInfo.userId.toNumber(),
  });
}
